package com.example.mq.mqtt;

import com.alibaba.fastjson.JSONObject;
import com.example.mq.config.MqttParamConfig;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;


@Component
public class MqttConnectUtils {
    private Logger logger = LoggerFactory.getLogger(MqttConnectUtils.class);
    @Resource
    private MqttParamConfig mqttParamConfig;
    private MqttClient mqttClient;
    public MqttClient creteMqttClient(){
        return  this.initClient();
    }
    @PostConstruct
    public void connect(){
        try {
            logger.info("getBrokerUrl{}",mqttParamConfig.getBrokerUrl());
            logger.info("mqttParamConfig.getOptions(){}", JSONObject.toJSONString(mqttParamConfig.getOptions()));
            this.initClient().connect(mqttParamConfig.getOptions());
            logger.info("mqtt连接成功!");
        } catch (MqttException e) {
            e.printStackTrace();
            logger.error("mqtt连接失败!{}",e.getMessage());
        }
    }
    public  MqttClient  initClient(){
        try {
            mqttClient = new MqttClient(mqttParamConfig.getBrokerUrl(),mqttParamConfig.getClientId());
        } catch (MqttException e) {
            logger.error("mqtt实体创建失败!{}",e.getMessage());
        }
        return mqttClient;
    }
    /**
     *消息发布方法
     * @param topicName
     * @param message
     * @return
     */
    public boolean publish(String topicName, String message) {
        logger.info("订阅主题名:{}, message:{}", topicName, message);
        MqttMessage mqttMessage = new MqttMessage(message.getBytes(StandardCharsets.UTF_8));
        try {
            this.mqttClient.publish(topicName, mqttMessage);
            return true;
        }catch (MqttException exception){
            exception.printStackTrace();
            return false;
        }
    }


    public boolean publish(String topicName, int qos, String message) {
        logger.info("主题名:{}, qos:{}, message:{}", topicName, qos, message);
        MqttMessage mqttMessage = new MqttMessage(message.getBytes(StandardCharsets.UTF_8));
        try {
            this.mqttClient.publish(topicName, mqttMessage.getPayload(), qos, false);
            return true;
        }catch (MqttException exception){
            exception.printStackTrace();
            return false;
        }
    }

    /**
     * 订阅某个主题
     *
     * @param topicName
     * @param qos
     */
    public void subscribe(String topicName, int qos) {
        logger.info("订阅主题名:{}, qos:{}", topicName, qos);
        try {
            this.mqttClient.subscribe(topicName, qos);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    /**
     * 订阅某个主题
     *
     * @param topicName
     * @param qos
     */
    public void subscribe(String topicName, int qos, IMqttMessageListener messageListener) {
        logger.info("订阅主题名:{}, qos:{}, Listener类:{}", topicName, qos, messageListener.getClass());
        try {
            this.mqttClient.subscribe(topicName, qos, messageListener);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    /**
     * 取消订阅主题
     * @param topicName 主题名称
     */
    public void cleanTopic(String topicName) {
        logger.info("取消订阅主题名:{}", topicName);
        try {
            this.mqttClient.unsubscribe(topicName);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

}
